<?php

class PasswordChangeForm extends CFormModel {
	public $currentPassword;
	public $newPassword;
	public $confirmPassword;

	/**
	 * Declares the validation rules.
	 */

	public function rules() {
		return array(
			array(
				'currentPassword, newPassword, confirmPassword',
				'required'
			),
			// email has to be a valid email address
			array(
				'confirmPassword',
				'compare',
				'compareAttribute' => 'newPassword',
			),
			array(
				'newPassword',
				'length',
				'min' => 4
			),
			array(
				'currentPassword',
				'checkCurrentPassword',
			),
		);
	}

	/**
	 * Declares customized attribute labels.
	 * If not declared here, an attribute would have a label that is
	 * the same as its name with the first letter in upper case.
	 */
	public function attributeLabels() {
		return array(
			'currentPassword' => '当前密码',
			'newPassword' => '新密码',
			'confirmPassword' => '确认新密码',
		);
	}

	public function checkCurrentPassword($attribute, $params) {
		if (!$this->hasErrors()) {
			$identity = new UserIdentity(Yii::app()->user->name, $this->currentPassword);
			if (!$identity->authenticate())
				$this->addError('currentPassword', '当前密码不正确');
		}
	}
}
